package com.le.buddha.conf;

import cn.hutool.core.util.ObjectUtil;
import com.le.buddha.common.ApplicationContextHolder;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * @author guole
 * @created 2024/6/17
 */
public class UserUtil {

    public static Long   getUserId(HttpServletRequest request){

        RedisTemplate redisTemplate= ApplicationContextHolder.getBean(StringRedisTemplate.class);
        String token = request.getHeader("TOKEN_USER");
        Long userId=null;
        if(token!=null){
          Object object=  redisTemplate.opsForValue().get(RedisKey.userToken + token);
          if(ObjectUtil.isNotNull(object)){
              userId=Long.parseLong(redisTemplate.opsForValue().get(RedisKey.userToken + token).toString()) ;

          }

        }
       return userId;
    }
    public static String   getToken(HttpServletRequest request){

        RedisTemplate redisTemplate= ApplicationContextHolder.getBean(StringRedisTemplate.class);
        String token = request.getHeader("TOKEN_USER");
         if(ObjectUtil.isNotNull(token)&&redisTemplate.hasKey(RedisKey.userToken + token)){

            return token;
        }
        return null;
    }
}
